import { getPermissionMenu } from "@/api/welcome";
import data from "@/router/data";
import recursonRoutes from "@/router/recursonRoutes";
import dynamicRoutes from "@/router/dynamicRoutes";
import router from "@/router/index";
let routes = data.find((item) => item.name === "/");

export default {
  namespaced: true,
  state: {
    menus: [],
  },
  getters: {},
  mutations: {
    SET_MENUS(state, payload) {
      state.menus = [...payload];
      dynamicRoutes.children = [...payload];
      router.addRoute(dynamicRoutes);
    },
    DEL_MENUS(state) {
      state.menus = [];
    },
  },
  actions: {
    async FETCH_MENUS(context) {
      let res = await getPermissionMenu();
      console.log(routes.children);
      let arr = recursonRoutes(res.data.menuList, routes.children);
      // mutations.SET_MENUS(arr);
      console.log(arr);
      return context.commit("SET_MENUS", arr);
    },
    // FETCH_MENUS({ commit }) {
    //   return getPermissionMenu().then((res) => {
    //     console.log(res.data.menuList);
    //     let menus = recursonRoutes(res.data.menuList, routes.children);
    //     commit("SET_MENUS", menus);
    //   });
    // },
  },
};
